<template>
  <el-dialog
    v-model="is_show"
    :close-on-click-modal="false"
    append-to-body
    :title="workFrom.title"
    width="600"
    class="dialogForm"
    :before-close="beforeClose"
  >
    <el-form
      :model="addFrom"
      :rules="rulesForm"
      label-width="110px"
      ref="ruleFormRef"
    >
      <el-form-item label="指 派 给:" prop="belongId">
        <el-select
          clearable
          v-model="addFrom.belongId"
          placeholder="请选择人员"
          @focus="userInputFocus"
          multiple
          class="w-full"
        >
          <el-option
            v-for="(item, index) in selectOptionObj['belongId']"
            :key="index"
            :label="item.label"
            :value="item.uuid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          type="textarea"
          placeholder="请输入内容"
          v-model="addFrom.remark"
          maxlength="300"
          :rows="4"
          show-word-limit
        >
        </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="beforeClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="btnLoading">
          保存
        </el-button>
      </span>
    </template>
    <!-- 弹出框下拉框 -->
    <SelectDialog
      :dialog-param="dialogParam"
      @ok="selectDialogData"
      @close="closeFn"
    ></SelectDialog>
  </el-dialog>
</template>
<script setup name="taskWork">
import { getTaskDeal } from '@/api/taskManage/task'
import { addManage } from '@/api/system/notice'
import { reactive, getCurrentInstance, toRefs } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import useUserStore from '@/store/modules/user'
import useCommonStore from '@/store/modules/common'
const { proxy } = getCurrentInstance()
// 通用数据store
const router = useRouter()
const route = useRoute()
const commonStore = useCommonStore()
const userStore = useUserStore()
const is_show = computed(() => {
  return true
})
const data = reactive({
  addFrom: {
    belongId: '',
    remark: ''
  },
  selectOptionObj: {}, // select
  dialogParam: {},
  gettime: '',
  // 表单校验
  rulesForm: {
    belongId: [{ required: true, message: '请选择人员', trigger: 'blur' }]
  }
})
const { selectOptionObj, addFrom, dialogParam, rulesForm, gettime } =
  toRefs(data)
const props = defineProps({
  workFrom: {
    type: Object
  }
})
const btnLoading = ref(false) //按钮加载状态
/**
 * 弹出框关闭
 * @param {*} val
 */
const closeFn = val => {
  dialogParam.value.isDialog = false
}

/**
 * 接收人获取焦点事件
 */
function userInputFocus() {
  const user = useUserStore().user['orgId']
  dialogParam.value = {
    type: 'belongId',
    title: '请选择人员',
    dataType: 'person',
    nodeKey: 'uuid',
    multiple: true,
    checkStrictly: false
  }
  dialogParam.value.isDialog = true
  dialogParam.value.apiParams = {
    disabled: 'false',
    deptId: user == '484' ? '100' : user
    // deptId:'484'
  }
  dialogParam.value.defaultCheckedKeys = [addFrom.value.belongId] || []
}
/** 弹出框下拉框
 * 接收人
 * @param {*} val  selectDialog组件返回的值
 */
const selectDialogData = val => {
  switch (val.type) {
    case 'belongId':
      selectOptionObj.value['belongId'] = val.selectData
      addFrom.value.belongName = val.labelData.toString()
      addFrom.value.belongId = val.defaultCheckedKeys
        ? val.defaultCheckedKeys
        : ''
      break
  }
}
//关闭
function beforeClose() {
  proxy.$emit('closeForm')
}
//获取当前时间
function getTime() {
  var _this = this
  let yy = new Date().getFullYear()
  var mm =
    new Date().getMonth() > 9
      ? new Date().getMonth() + 1
      : new Date().getMonth() == 9
      ? new Date().getMonth() + 1
      : '0' + (new Date().getMonth() + 1)
  var dd =
    new Date().getDate() < 10
      ? '0' + new Date().getDate()
      : new Date().getDate()
  let hh = new Date().getHours()
  let mf =
    new Date().getMinutes() < 10
      ? '0' + new Date().getMinutes()
      : new Date().getMinutes()
  let ss =
    new Date().getSeconds() < 10
      ? '0' + new Date().getSeconds()
      : new Date().getSeconds()
  gettime.value = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
  console.log('gettime', gettime.value)
}
//保存
function handleSubmit() {
  proxy.$refs['ruleFormRef'].validate(valid => {
    if (valid) {
      let params = {}
      params.remark = addFrom.value.remark
      params.id = props.workFrom.operateId
      params.targetId = addFrom.value.belongId
      params.targetName = addFrom.value.belongName
      params.taskId = props.workFrom.taskId
      params.dealStatus = 2 //分配工单
      if (params.targetId.length > 0) {
        for (let i = 0; i < params.targetId.length; i++) {
          params.targetId[i] = params.targetId[i].replace('U', '')
        }
      }
      params.targetId = params.targetId ? params.targetId.toString() : ''
      btnLoading.value = true
      let array = []
      getTaskDeal(params)
        .then(res => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess('操作成功')
            array = res.data
            for (let i = 0; i < array.length; i++) {
              let mesVoData = {
                businessId: '分配工单-通知-' + array[i].operateId, //业务ID
                content: `${props.workFrom.titleName}:${params.remark}`,
                followDate: undefined, //跟进日期
                followType: undefined, //跟进方式
                impSign: props.workFrom.impSign, //重要程度:0-非常重要，1-重要，2-一般
                receiver: array[i].belongId, //接收人id
                receiverName: array[i].belongName, //接收人中文
                reminderMethod: '0', //提醒方式:0-系统站内，1-邮箱，2-短信，允许多个并存，例如:0,1,2
                sendTime: gettime.value, //发送时间
                sender: userStore.user.nickName, //发送人
                title: `您有一条'${props.workFrom.titleName}'已分配的任务待处理`, //待办标题
                type: 0, //待办类型:0-普通，1-流程
                serviceType: '10', //服务类型  0-项目，1-订单，2-客户，3-工单，4-任务，5-产品，6-合同，7-公告，8-日程，9-回款
                urgFlag: '2', //紧急程度:0-非常紧急，1-紧急，2-一般
                urlName: '前往处理', //待办链接名称(多链接名称用英文逗号隔开)
                url: `/rsgl/other/taskProcessing?id=${array[i].taskId}&operateId=${array[i].operateId}` //待办链接(多链接用英文逗号隔开)
              }
              console.log('mesVoData==>', mesVoData)
              addManage(mesVoData)
            }

            btnLoading.value = false
            router.push({
              path: '/rsgl/other/taskManage'
            })
            beforeClose()
          }
        })
        .catch(res => {
          btnLoading.value = false
        })
    }
  })
}

function init() {
  console.log('props', props.workFrom)
  getTime()
}
init()
</script>
